/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.report.controller;

import com.ipcc.lazyLoading.LazyAbstractDataModel;
import com.ipcc.manager.entity.IPCC_EXTENSIONS;
import com.ipcc.report.entity.SUM_FS_CDR;
import com.telsoft.applet.util.StringUtil;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.model.LazyDataModel;
import telsoft.cms_admin.login.LoginBean;

/**
 *
 * @author Administrator
 */
@ViewScoped
public class SUM_FS_CDRController implements Serializable {

    private Date tuNgay;
    private Date denNgay;
    private String extension;
    private String time;
    private List<IPCC_EXTENSIONS> listIE = new ArrayList<IPCC_EXTENSIONS>();
    private List<String> chitiet = new ArrayList<String>();
    private ArrayList<SUM_FS_CDR> list = new ArrayList<SUM_FS_CDR>();
    private LazyDataModel<SUM_FS_CDR> lazyModel;
    private boolean is_admin = false;
    private String user_id = "";
    /**
     * Creates a new instance of SUM_FS_CDRController
     */
    public SUM_FS_CDRController() {
        try {
            is_admin = (Boolean) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.IS_ADMIN);
            if(is_admin){
                listIE = IPCC_EXTENSIONS.getAll("",null);
            }
            else {
                user_id = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(LoginBean.USER_ID).toString();
                listIE = IPCC_EXTENSIONS.getAll("",user_id);
            }            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public LazyDataModel<SUM_FS_CDR> getLazyModel() {
        return lazyModel;
    }

    public void setLazyModel(LazyDataModel<SUM_FS_CDR> lazyModel) {
        this.lazyModel = lazyModel;
    }

    public boolean isIs_admin() {
        return is_admin;
    }

    public void setIs_admin(boolean is_admin) {
        this.is_admin = is_admin;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public Date getTuNgay() {
        return tuNgay;
    }

    public void setTuNgay(Date tuNgay) {
        this.tuNgay = tuNgay;
    }

    public Date getDenNgay() {
        return denNgay;
    }

    public void setDenNgay(Date denNgay) {
        this.denNgay = denNgay;
    }

    public String getExtension() {
        return extension;
    }

    public void setExtension(String extension) {
        this.extension = extension;
    }

    public List<IPCC_EXTENSIONS> getListIE() {
        return listIE;
    }

    public void setListIE(List<IPCC_EXTENSIONS> listIE) {
        this.listIE = listIE;
    }

    public List<String> getChitiet() {
        return chitiet;
    }

    public void setChitiet(List<String> chitiet) {
        this.chitiet = chitiet;
    }

    public ArrayList<SUM_FS_CDR> getList() {
        return list;
    }

    public void setList(ArrayList<SUM_FS_CDR> list) {
        this.list = list;
    }

    public boolean checkInput() {
        if (denNgay.compareTo(tuNgay) < 0) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Từ ngày không được lớn hơn đến ngày!"));
            return false;
        }
        return true;
    }

    public String thongke() {
        try {
            if (checkInput()) {
                if (chitiet.size() == 0) {
                    list = SUM_FS_CDR.getAll(" where sum_date >= to_date('" + StringUtil.format(tuNgay, "dd-MM-yyyy") + "','dd-mm-yyyy') and sum_date < to_date('" + StringUtil.format(denNgay, "dd-MM-yyyy") + "','dd-mm-yyyy') + 1 and (extension = '" + extension + "' or '" + extension + "' = '-6666') ", "mm");
                    time = StringUtil.format(tuNgay, "dd-MM-yyyy") + " -> " + StringUtil.format(denNgay, "dd-MM-yyyy");
                } else {
                    list = SUM_FS_CDR.getAll(" where sum_date >= to_date('" + StringUtil.format(tuNgay, "dd-MM-yyyy") + "','dd-mm-yyyy') and sum_date < to_date('" + StringUtil.format(denNgay, "dd-MM-yyyy") + "','dd-mm-yyyy') + 1 and (extension = '" + extension + "' or '" + extension + "' = '-6666')", "dd");
                }
                lazyModel = new LazyAbstractDataModel<SUM_FS_CDR>(list);
            }
            if (list.size() > 0) {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có " + list.size() + " bản ghi được thống kê!"));
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Không có bản ghì nào được thống kê!"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Có lỗi xảy ra trong quá trình xử lí!"));
        }
        return "";
    }

    public String makeTime(int seconds) {
        int day = (int) TimeUnit.SECONDS.toDays(seconds);
        long hours = TimeUnit.SECONDS.toHours(seconds) - (day * 24);
        long minute = TimeUnit.SECONDS.toMinutes(seconds) - (TimeUnit.SECONDS.toHours(seconds) * 60);
        long second = TimeUnit.SECONDS.toSeconds(seconds) - (TimeUnit.SECONDS.toMinutes(seconds) * 60);

        String time = String.format("%02d:%02d:%02d", hours, minute, second);
        return time;
    }
}
